﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.Entity;
using Model;

namespace Logic
{
   public class ItemsDataLogic
    {


       public List<Sys_ItemsDetail> GetList(string itemId = "", string keyword = "")
       {
           Model1Container db = new Model1Container();

           var expression = ExtLinq.True<Sys_ItemsDetail>();
           if (!string.IsNullOrEmpty(itemId))
           {
               expression = expression.And(t => t.F_ItemId == itemId);
           }
           if (!string.IsNullOrEmpty(keyword))
           {
               expression = expression.And(t => t.F_ItemName.Contains(keyword));
               expression = expression.Or(t => t.F_ItemCode.Contains(keyword));
           }

           return db.Set<Sys_ItemsDetail>().Where(expression).OrderBy(t => t.F_SortCode).ToList();
       }
       public List<Sys_ItemsDetail> GetItemList(string enCode)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append(@"SELECT  d.*
                            FROM    Sys_ItemsDetail d
                                    INNER  JOIN Sys_Items i ON i.F_Id = d.F_ItemId
                            WHERE   1 = 1
                                    AND i.F_EnCode = @enCode
                                    AND d.F_EnabledMark = 1
                                    AND d.F_DeleteMark = 0
                            ORDER BY d.F_SortCode ASC");
           DbParameter[] parameter = 
            {
                 new SqlParameter("@enCode",enCode)
            };
           Model1Container db = new Model1Container();
           return db.Database.SqlQuery<Sys_ItemsDetail>(strSql.ToString(), parameter).ToList<Sys_ItemsDetail>();
       }
       public Sys_ItemsDetail GetForm(string keyValue)
       {
           return new Model1Container().Set<Sys_ItemsDetail>().Find(keyValue);
       }
       public void DeleteForm(string keyValue)
       {
           Model1Container dbcontext = new Model1Container();
           var entitys = dbcontext.Set<Sys_ItemsDetail>().Where(t => t.F_Id == keyValue).ToList();
           entitys.ForEach(m => dbcontext.Entry<Sys_ItemsDetail>(m).State = EntityState.Deleted);
           dbcontext.SaveChanges();
       }
       public void SubmitForm(Sys_ItemsDetail itemsDetailEntity, string keyValue)
       {
            try
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    using (Model1Container db = new Model1Container())
                    {
                        itemsDetailEntity.F_Id = keyValue;
                        db.Sys_ItemsDetail.Attach(itemsDetailEntity);
                        db.Entry(itemsDetailEntity).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                }
                else
                {
                    using (Model1Container db = new Model1Container())
                    {
                        itemsDetailEntity.F_Id = Guid.NewGuid().ToString();
                        db.Sys_ItemsDetail.Add(itemsDetailEntity);
                        db.SaveChanges();
                    }
                }
            }
            catch(Exception ex)
            {


            }
       }

       public List<Sys_ItemsDetail> GetDicDetailsListByParentID(string ParentID)
       {
           Model1Container db = new Model1Container();
           return db.Set<Sys_ItemsDetail>().Where(c => c.F_ParentId == ParentID).ToList();
       }
    }
}
